package com.zhijiang.hospital.service.impl;

import com.zhijiang.common.util.HttpPost;

import com.zhijiang.common.util.jwt.JwtTokenUtils;
import com.zhijiang.common.util.redis.RedisCache;
import com.zhijiang.common.util.redis.RedisConstants;
import com.zhijiang.hospital.entrty.dto.EmployeeDto;
import com.zhijiang.hospital.entrty.dto.HisUserDto;
import com.zhijiang.hospital.mapper.EmployeeDtoMapper;
import com.zhijiang.hospital.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author 75936
 * @version 1.0
 * @project meeting-backstage
 * @description
 * @date 2023-02-08 10:17:50
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    EmployeeDtoMapper employeeDtoMapper;
    @Autowired
    RedisCache redisCache;

    @Override
    public EmployeeDto login(@RequestBody EmployeeDto employeeDto) {
        HisUserDto hisUserDto = HttpPost.validateUser(employeeDto);
        if (hisUserDto.getCheckUserResult()){
            EmployeeDto oneEmployeeDto = employeeDtoMapper.findOneEmployeeDto(employeeDto.getEmplCode());
            String token = JwtTokenUtils.getToken(oneEmployeeDto.getEmplCode(), "zhijiang");
            redisCache.setCacheObject(RedisConstants.LOGIN+oneEmployeeDto.getEmplCode(), oneEmployeeDto,RedisConstants.LOGIN_CODE_TTL2, RedisConstants.hours);
            oneEmployeeDto.setToken(token);
            return oneEmployeeDto;
        }else {
          throw new RuntimeException("账号或密码不正确");
        }
    }
}
